//
//  textViewController.m
//  Office
//
//  Created by hztech on 2018/1/1.
//  Copyright © 2018年 lxy. All rights reserved.
//

#import "textViewController.h"

@interface textViewController ()<UITextViewDelegate>

@property(strong,nonatomic)UITextView *textView;
@property(strong,nonatomic)UILabel *count;

@end

@implementation textViewController
//全局变量
{
    UILabel *Lab;
    UITextField *textField;
    UITextField *textField1;
    UIButton *btn;
    int type;
}
//重写init
-(instancetype)init{
    self = [super init];
    if (self)
    {
        [self setToptittle:@"日志登记"];
        [self setTopTittleColor:[UIColor whiteColor]];
        [self setTopBgColor:headColor];
        [self setBackBtnImage:[UIImage imageNamed:@"wo_merchandise_detail_back@3x"]];
        [self setBackBtnHide:NO];
        type = 0;
        self.view.backgroundColor = graybackgroundColor;
    }
    return self;
}
//主函数---UI界面搭建
- (void)viewDidLoad {
    [super viewDidLoad];
    //日期
    Lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 60, SCREENWIDTH, 40)];
    
    Lab.font=[UIFont systemFontOfSize:17];
    Lab.textAlignment=NSTextAlignmentCenter;
    Lab.textColor=[UIColor blackColor];
    [self.view addSubview:Lab];
    NSLog(@"%@",self.today);
    //岗位
    UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(Lab.frame)-10, ScreenWidth-40, 40)];
    lab1.text=[NSString stringWithFormat:@"岗位:           %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"user_job"]];
    lab1.font=[UIFont systemFontOfSize:17];
    lab1.textAlignment=NSTextAlignmentLeft;
    lab1.textColor=[UIColor blackColor];
    [self.view addSubview:lab1];
    
    //项目
    UILabel *lab2=[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(lab1.frame)+15, 50, 40)];
    lab2.text=@"项目:";
    lab2.font=[UIFont systemFontOfSize:17];
    lab2.textAlignment=NSTextAlignmentLeft;
    lab2.textColor=[UIColor blackColor];
    [self.view addSubview:lab2];
    
    textField = [[UITextField alloc] initWithFrame:CGRectMake(100, CGRectGetMaxY(lab1.frame)+15, ScreenWidth-120, 40)];
    textField.returnKeyType = UIReturnKeyDone; //设置按键类型
    textField.enablesReturnKeyAutomatically = NO;
    textField.textColor = [UIColor blackColor];
    textField.font = [UIFont systemFontOfSize:14];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.placeholder = @"请输项目名称";
    textField.secureTextEntry = NO;
    textField.borderStyle = UITextBorderStyleLine;
    textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:textField];
    
    //日志内容
    UILabel *lab3=[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(lab2.frame)+30, ScreenWidth-40, 40)];
    lab3.text=@"日志内容:";
    lab3.font=[UIFont systemFontOfSize:17];
    lab3.textAlignment=NSTextAlignmentLeft;
    lab3.textColor=[UIColor blackColor];
    [self.view addSubview:lab3];
    [self.view addSubview:self.textView];
    
    
    //工时
    
    UILabel *lab4=[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(_textView.frame)+30, ScreenWidth-40, 40)];
    lab4.text=@"工时:";
    lab4.font=[UIFont systemFontOfSize:17];
    lab4.textAlignment=NSTextAlignmentLeft;
    lab4.textColor=[UIColor blackColor];
    [self.view addSubview:lab4];
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(100, CGRectGetMaxY(_textView.frame)+30, ScreenWidth-120, 40)];
    textField1.returnKeyType = UIReturnKeyDone; //设置按键类型
    textField1.enablesReturnKeyAutomatically = NO;
    textField1.textColor = [UIColor blackColor];
    textField1.font = [UIFont systemFontOfSize:14];
    textField1.textAlignment = NSTextAlignmentLeft;
    textField1.text = @"7.5";
    textField1.secureTextEntry = NO;
    textField1.borderStyle = UITextBorderStyleLine;
    textField1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:textField1];
    
    //确认
    btn=[[UIButton alloc]initWithFrame:CGRectMake(40, CGRectGetMaxY(textField1.frame)+45, ScreenWidth-80, 40)];
    [btn setTitle:@"确认提交" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize: 17];
    btn.layer.cornerRadius = 8.0;
    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    btn.backgroundColor = [UIColor orangeColor];
    [btn addTarget:self action:@selector(Submit) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btn];
}
#pragma mark  - 对外函数
//日期
-(void)takeDate{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    Lab.text=[dateFormatter stringFromDate:self.today];
}
//不可修改状态
-(void)takeDaily{
    textField.text = self.hxt.daily_project;
    //NSLog(@"%@",self.hxt.daily_project);
    textField.enabled = NO;
    textField.backgroundColor = [UIColor clearColor];
    textField1.text = self.hxt.daily_time;
    textField1.enabled = NO;
    textField1.backgroundColor = [UIColor clearColor];
    _textView.text = self.hxt.daily_string;
    _textView.editable = NO;
    _textView.backgroundColor = [UIColor clearColor];
    _count.text = [NSString stringWithFormat:@"%lu/100", (unsigned long)_textView.text.length  ];
    btn.enabled =NO;
    type = 1;
    
    UIButton *changeBtn=[[UIButton alloc]initWithFrame:CGRectMake(SCREENWIDTH-60, 20, 40, 40)];
    [changeBtn setTitle:@"修改" forState:UIControlStateNormal];
    changeBtn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    changeBtn.layer.cornerRadius = 8.0;
    changeBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    [changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [changeBtn addTarget:self action:@selector(changeType) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:changeBtn];
}
#pragma mark  - 修改
//修改当前状态
-(void)changeType{
    textField.enabled = YES;
    textField.backgroundColor = [UIColor whiteColor];
    textField1.enabled = YES;
    textField1.backgroundColor = [UIColor whiteColor];
    _textView.editable = YES;
    _textView.backgroundColor = [UIColor whiteColor];
    [btn setEnabled:YES];
}
#pragma mark  - 加数据
//数据传输
//判断是第一次传输还是修改已有数据
-(void)Submit{
    NSLog(@"成功");
    if ([_textView.text isEqualToString:@"请输入问题、解决方案或具体工作内容，不能少于10个字"]||_textView.text.length <10)
    {
        DSToast *toast=[[DSToast alloc]initWithText:@"日志内容不能为空或少于十个字"];
        [toast showInView:self.view];
        return;
    }
    if ([textField1.text isEqualToString:@""]) {
        DSToast *toast=[[DSToast alloc]initWithText:@"工时不能为空"];
        [toast showInView:self.view];
        return;
    }
    if (type==1) {
        //BmobQuery   *bquery = [BmobQuery queryWithClassName:@"daily"];
        //查找GameScore表里面id为0c6db13c的数据
        NSLog(@"%@",self.hxt.daily_id);

        
        BmobObject  *object = [BmobObject objectWithoutDataWithClassName:@"daily" objectId:self.hxt.daily_id];
        [object setObject:textField.text forKey:@"daily_project"];
        [object setObject:_textView.text forKey:@"daily_string"];
        [object setObject:textField1.text forKey:@"daily_time"];
        [object updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                self.confirmBlock(self.today);
                [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
                NSLog(@"%@",object);
            } else {
                NSLog(@"%@",error);
            }
        }];
    } else {
        BmobObject *obj = [[BmobObject alloc] initWithClassName:@"daily"];
        NSString *str=[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"];
        [obj setObject:str forKey:@"user_id"];
        [obj setObject:self.today forKey:@"daily_date"];
        
        [obj setObject:textField.text forKey:@"daily_project"];
        [obj setObject:_textView.text forKey:@"daily_string"];
        [obj setObject:textField1.text forKey:@"daily_time"];
        
        
        [obj saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            NSLog(@"%@",error);
            if (!error) {
                NSLog(@"提交成功");
                self.confirmBlock(self.today);
                [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
                
            }else{
                DSToast *toast=[[DSToast alloc]initWithText:[NSString stringWithFormat:@"%@",error]];
                [toast showInView:self.view];
            }
            
        }];
    }
    
    
}
#pragma mark  - 懒加载
//日志文字框协议代理
-(UITextView *)textView{
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(100, CGRectGetMaxY(textField.frame)+30, ScreenWidth-120, 150)];
        //https://www.jianshu.com/p/14385b1db4f3
        _textView.text = @"请输入问题、解决方案或具体工作内容，不能少于10个字";
        _textView.font = [UIFont fontWithName:@"Arial" size:16.5f];
        _textView.textColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0f];
        //_textView.backgroundColor = [UIColor clearColor];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.autocorrectionType = UITextAutocorrectionTypeNo;
        
        _textView.layer.borderColor = [UIColor blackColor ].CGColor;
        _textView.layer.borderWidth = 1;
        _textView.layer.cornerRadius =5;
        
        _textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
        
        _textView.keyboardType = UIKeyboardTypeDefault;
        
        _textView.returnKeyType = UIReturnKeyDefault;
        
        _textView.scrollEnabled = YES;
        _textView.delegate = self;

        _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

        _count = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100, CGRectGetMaxY(_textView.frame) +5, 60, 20)];
        _count.text = @"0/100";
        _count.textAlignment = NSTextAlignmentRight;
        _count.font = [UIFont fontWithName:@"Arial" size:15.0f];
        _count.backgroundColor = [UIColor clearColor];
        _count.textColor = [UIColor redColor];
        _count.enabled = NO;
        
        [self.view addSubview:_count];
        
    }
    return _textView;
}
#pragma mark  - menu协议
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
    
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"请输入问题、解决方案或具体工作内容，不能少于10个字"]) {
        
        textView.text = @"";
        
    }
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    
    return YES;
    
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length <1) {
        textView.text = @"请输入问题、解决方案或具体工作内容，不能少于10个字";
    }
    
}



- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location < 100)
    {
        return  YES;
        
    } else  if ([textView.text isEqualToString:@"\n"]) {
        
        //这里写按了ReturnKey 按钮后的代码
        return NO;
    }
    
    if (textView.text.length == 100) {
        
        return NO;
    }
    
    return YES;
    
}

- (void)textViewDidChange:(UITextView *)textView
{
    _count.text = [NSString stringWithFormat:@"%lu/100", (unsigned long)textView.text.length  ];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
